<script>

var previousUserID=0;//most recently selected user's ID

/*
* show edit user info window, fill in the user's current info
*/
function showEditUsersInfoWindow(user)
{
	if(user!=null)
	{
		$('#edit-shadow-area').toggle();
		$('#edit-area').toggle();
//		$('input').attr("disabled", true);
		document.getElementById('error-area').innerHTML='';
		document.getElementById('user_ID').value=user.user_ID;
		$("input[name='contact_first_name']").val(user.contact_first_name);
		$("input[name='contact_last_name']").val(user.contact_last_name);
		$("input[name='phoneNumber']").val(user.phoneNumber);
		$("input[name='emailAddress']").val(user.emailAddress);
		var element=document.getElementById('user_role_type_ID');
		for(var i=0;i<element.options.length; i++)
		{
			if(element.options[i]==user.user_role_type_ID)
				element.selectedIndex=i;
		}
		$("input[name='user_name']").val(user.user_name);
		document.getElementById('password').value='';
		document.getElementById('password2').value='';
	}
}

/*
* close edit users info window
*/
function closeEditUsersInfoWindow() {
	$('#edit-shadow-area').toggle();
	$('#edit-area').toggle();
}

/*
* show add user info window
*/
function showAddUsersInfoWindow() {
	var prefix='adduser_';
	document.getElementById('adduser-error-area').innerHTML='';
	document.getElementById(prefix+'contact_first_name').value='';
	document.getElementById(prefix+'contact_last_name').value='';
	document.getElementById(prefix+'phoneNumber').value='';
	document.getElementById(prefix+'emailAddress').value='';
	var element=document.getElementById(prefix+'user_role_type_ID');
	element.selectedIndex=0;
	document.getElementById(prefix+'user_name').value='';
	document.getElementById(prefix+'password').value='';
	document.getElementById(prefix+'password2').value='';
	$('#add-shadow-area').toggle();
	$('#add-area').toggle();
	return false;
}

/*
* close add user info window
*/
function closeAddUsersInfoWindow() {
	$('#add-shadow-area').toggle();
	$('#add-area').toggle();
}

/*
* confirm with user if he wants to edit that user's info
*/
function toEditUser(userID)
{
	if(userID==0)
	{
		alert("You didn't select a row.");
	}
	return false;
}

/*
* confirm with user if he wants to delete that user
*/
function toDeleteUser(userID)
{
	var message='';
	if(userID==0)
	{
		message+='You didn\'t select a row.';
	}
	else
		message+='Do you want to delete the user with UserID '+userID+'?';
	var toDelete=window.confirm(message);
	if(toDelete)
	{
		return false;
	}
	else
	{
		previousUserID=0;//reset previousUserID
		return false;
	}
}

/*
* operations after deleting user
* update grid view if succeed
*/
function afterDelete(data)
{
	if(data=='delete-success')
	{
		previousUserID=0;
		$.fn.yiiGridView.update("otherusersTable");//update grid view;
	}
}

function afterEdit(user)
{
	showEditUsersInfoWindow(user);
}

$(document).ready(function ()
{
	$('#edit-button').click(function (){
     $.ajax({
       url: '<?php echo CController::createUrl('admin/editUser');?>',
       type: 'post',
       dataType: 'json',
       data: {userID:previousUserID},
       success: afterEdit
     });
   });
  
   $('#delete-button').click(function (){
     $.ajax({
       url: '<?php echo CController::createUrl('admin/deleteUser');?>',
       type: 'post',
       dataType: 'json',
       data: {userID:previousUserID},
       success: afterDelete
     });
   });

});


</script>

<?php $usermanager_form = $this->beginWidget('CActiveForm', array(
		'id'=>'usermanager-form',
		'enableClientValidation'=>true,
	//	'enableAjaxValidation'=>true,
		));
?>
<?php
// user info grid view
CActiveForm::widget('zii.widgets.grid.CGridView', array(
			    	'dataProvider'=>$usersDataProvider,
			    	'id'=>'otherusersTable',
			    	'selectableRows'=>1,
			    	'selectionChanged'=>'function(id) {' .
			    		'var columnID = $.fn.yiiGridView.getSelection(id);'.
			    		'previousUserID=columnID;'
			    	.'}',
			    	'cssFile'=>Yii::app()->request->baseUrl.'/css/gridview.css',
					'columns'=>array(
			    		array(
							'name'=>'First Name',
							'value'=>'$data->contact_first_name',
						),
			    		array(
			    			'name'=>'Last Name',
			    			'value'=>'$data->contact_last_name',
			    		),
			    		array(
			    			'name'=>'Username',
			    			'value'=>'$data->user_name',
			    		),
			    		array(
			    			'name'=>'Password',
			    			'value'=>'$data->password',
			    		),
			    		array(
			    			'name'=>'Phone',
			    			'value'=>'$data->phoneNumber',
			    		),
			    		array(
			    			'name'=>'Email',
			    			'value'=>'$data->emailAddress',
			    		),
			    	),
				),
				false//captureOutput
			);
?>

 <div align='center'>
	<input src='<?php echo Yii::app()->request->baseUrl?>/images/add.jpg' type='image' id='add-button' onclick="return showAddUsersInfoWindow()"/>
 	<input src='<?php echo Yii::app()->request->baseUrl?>/images/edit.jpg' type='image' id='edit-button' onclick='return toEditUser(previousUserID);'/>
	<input src='<?php echo Yii::app()->request->baseUrl?>/images/delete.jpg' type='image' id='delete-button' onclick='return toDeleteUser(previousUserID);' />
 </div>
 
 <div id="edit-shadow-area" class="black-overlay" onclick="closeEditUsersInfoWindow()"></div>
<div class="white-content" id="edit-area">
<div style="cursor: pointer; color: #000; position: absolute; right: 10px; z-index: 100;" onclick="closeEditUsersInfoWindow()">Close [X]</div>
	<?php 
		$user = new Users;
		$user->scenario = 'update';
		$this->renderPartial('userinfo', array('model'=>$user)); ?>
</div>
<div id="add-shadow-area" class="black-overlay" onclick="closeAddUsersInfoWindow()"></div>
<div class="white-content" id="add-area">
<div style="cursor: pointer; color: #000; position: absolute; right: 10px; z-index: 100;" onclick="closeAddUsersInfoWindow()">Close [X]</div>
	<?php
		$newUser = new Users;
		$newUser->scenario = 'register';
		$this->renderPartial('adduser', array('model'=>$newUser)); ?>
</div>

<?php $this->endWidget();?>

